import json
from lxml import etree

import requests;
import time

from bs4 import BeautifulSoup
from twisted.web.http import urlparse, parse_qs

from com.py.test.reptleDolinGoodMsg.RequestReptle.TMReptle.TM_DbOperate import TM_DbOperate

header={
    # "cookie":"cna=EYnEFeatJWUCAbfhIw4Sd0GO; x=__ll%3D-1%26_ato%3D0; hng=CN%7Czh-CN%7CCNY%7C156; uc1=cookie14=UoTaHYecARKhrA%3D%3D; uc3=vt3=F8dBy32hRyZzP%2FF7mzQ%3D&lg2=U%2BGCWk%2F75gdr5Q%3D%3D&nk2=1DsN4FjjwTp04g%3D%3D&id2=UondHPobpDVKHQ%3D%3D; t=ad1fbf51ece233cf3cf73d97af1b6a71; tracknick=%5Cu4F0F%5Cu6625%5Cu7EA22013; lid=%E4%BC%8F%E6%98%A5%E7%BA%A22013; uc4=nk4=0%401up5I07xsWKbOPxFt%2BwuLaZ8XIpO&id4=0%40UOE3EhLY%2FlTwLmADBuTfmfBbGpHG; lgc=%5Cu4F0F%5Cu6625%5Cu7EA22013; enc=ieSqdE6T%2Fa5hYS%2FmKINH0mnUFINK5Fm1ZKC0431E%2BTA9eVjdMzX9GriCY%2FI2HzyyntvFQt66JXyZslcaz0kXgg%3D%3D; _tb_token_=536fb5e55481b; cookie2=157aab0a58189205dd5030a17d89ad52; _m_h5_tk=150df19a222f0e9b600697737515f233_1565931936244; _m_h5_tk_enc=909fba72db21ef8ca51c389f65d5446c; otherx=e%3D1%26p%3D*%26s%3D0%26c%3D0%26f%3D0%26g%3D0%26t%3D0; l=cBa4gFrRqYHNUtVvBOfiquI8a17O4IJ51sPzw4_G2ICP9B5DeMDOWZezto8kCnGVL6mpR3RhSKO4BYTKIPaTlZXRFJXn9MpO.; isg=BI6ORhr9X6-NrOuY33d_XmZFy2SQp1Ju1qe4XLjXJRHsGyp1IJ9IG0kdUwfSA0oh",
    "referer":"https://detail.tmall.com/item.htm",
    "user-agent":"Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.87 UBrowser/6.2.4098.3 Safari/537.36",
    "cookie":"cna=h7/WExR98HMCAbck5+fn2y1y; x=__ll%3D-1%26_ato%3D0; _m_h5_tk=076ba86f8d9524f15984971fd99cbd09_1566015258288; _m_h5_tk_enc=30160c247cc5f2ac08980a480cc98830; x5sec=7b22726174656d616e616765723b32223a22633638336361366235656663616165306637636363646538323538633835333243492f4233656f46454e485670373362307253517667453d227d; otherx=e%3D1%26p%3D*%26s%3D0%26c%3D0%26f%3D0%26g%3D0%26t%3D0; hng=CN%7Czh-CN%7CCNY%7C156; uc3=lg2=WqG3DMC9VAQiUQ%3D%3D&nk2=AHYzkqrWU969%2FA6z&id2=UNDQTf%2BC0nyNhA%3D%3D&vt3=F8dBy3Kw2V4Uhvyq77Y%3D; t=0963ac6be3d67c0fedd81a404a7c94ef; tracknick=clq516138253; lid=clq516138253; uc4=id4=0%40UgcgsjW5sUYDTYseVCX%2BjQInX5ex&nk4=0%40AhhOD7FH%2BCboOdK55orpMefb9KrAesc%3D; lgc=clq516138253; enc=YfEO5fwsu8ngiigJcbnPQNse41%2BZD4FI47QjkHKd1w4kUzojFxac6jOlAM6hXEaZ3krHP5L%2BvTsGvnDZuWG56Q%3D%3D; _tb_token_=137986be587b; cookie2=199cdb6b86d151c69abb17d11635abcb; l=cBxildhcqi69XXoFBOCCVQKbL8bTlKdbjuSJGCx6i_5CZ18Cf1_Ok80JKeJ6cjWFT98B4iueOIwtBFHQ-ykfoTB7K9cdvdC..; isg=BJiYP2eVcSxth12tpvnTfrRZe8aGkf3pgXl6IdKJi1OAbTlXepMrmtULpeV4xrTj"
}
def getComments(itemId,goods_table_id,i):
    params = {"itemId":itemId,"sellerId":"2786465182","currentPage":i,"callback":"jsonp1350" }
    req = requests.get("https://rate.tmall.com/list_detail_rate.htm",params,headers=header).content.decode('utf-8')[12:-1];
    result=json.loads(req);
    rateDetail = result["rateDetail"]
    comment_list = rateDetail["rateList"]
    if not comment_list:
        return 111111
    # 数据插入操作！！
    data_list = []
    #插入数据
    commment_sql = "INSERT INTO `tb_a_assess` (goodsTableId,username,content,assesstime,productId) " \
                   "VALUES (%s, %s, %s, %s, %s)"
    productId= itemId
    for item in comment_list:
        content = item["rateContent"]
        user_name = item["displayUserNick"]
        creationTime =  item["rateDate"]
        #添加多行数据
        print(content,user_name)
        data_list.append((goods_table_id,user_name,content,creationTime,productId))
        # print(user_name,comment,userImage,score,creationTime)
    print(data_list)
    # dbo.execBySqlVals(commment_sql,data_list)

def getGoodsDetail(itemId,url):
    rurl = "https://mdskip.taobao.com/core/initItemDetail.htm"
    params = {"itemId":itemId,"callback":"jsonp1350" }
    res = requests.get(rurl,params,headers=header).content.decode('gbk')[12:-1]
    result=json.loads(res);
    result = result["defaultModel"]["itemPriceResultDO"]["priceInfo"]
    oldPrice = list(result.values())[0]["price"]
    try:
        price = list(result.values())[0]['promotionList'][0]["price"]
    except KeyError:
        price = 0
    body = requests.get(url)
    html = BeautifulSoup(body.text,"lxml")

    formList = html.find("form", {"id":"J_FrmBid"})
    title = formList.find('input',{'name':'title'}).get("value")
    shopname = formList.find('input',{'name':'seller_nickname'}).get("value")
    ulList = html.find('ul', {"id":"J_AttrUL"})
    brandname = ulList.find('li',{"id":"J_attrBrandName"})['title'].strip()
    liList = ulList.find_all('li')
    model = '无'
    productname='无'
    for li in  liList:
        if '型号' in li.text:
            model = li['title'].strip()
        if '产品名称' in li.text:
            productname = li['title'].strip()

    # 数据插入操作！！
    #插入数据
    goods_sql = "INSERT INTO `tb_g_goods` (title,name,brandname,shopname,price,model,productId,oldprice,goodsUrl) " \
                "VALUE (%s, %s, %s, %s, %s, %s, %s, %s, %s)"
    value =(title,productname,brandname,shopname,price,model,itemId,oldPrice,url)
    goods_table_id = dbo.execBySqlVal(goods_sql,value)
    return goods_table_id
    # print(model)
    # print(title)
    # print(shopname)
    # print(brandname)
    # print(productname)
    # print(price)
    # print(oldPrice)

def checkGoods(itemId):
    goods_sql = "SELECT 1 FROM `tb_g_goods` tbg WHERE tbg.productId = %s"
    res = dbo.execBySqlVal(goods_sql,itemId)
    if res:
        return 1
    return 0
dbo = TM_DbOperate()
# url = "https://item.taobao.com/item.htm?spm=a219r.lm895.14.1.54fc41cc4UVULv&id=592494727107&ns=1&abbucket=15"
# url = "https://detail.tmall.com/item.htm?spm=a220m.1000858.1000725.1.7e135399Joe28j&id=590655880239&skuId=4222870978770&areaId=440100&user_id=2200715826810&cat_id=2&is_b=1&rn=0965179336bd18b94a5c6febc3d09096"
# url = "https://detail.tmall.com/item.htm?spm=a220m.1000858.1000725.17.e0837908xESqp2&id=564892537894&skuId=3574398813814&areaId=440100&user_id=3108900529&cat_id=2&is_b=1&rn=dfea2cd86b57e8cc1fdefc69e06ca6a0"
url = 'https://detail.tmall.com/item.htm?spm=a1z10.3-b-s.w4011-14878130832.89.5ede2be2UCkOCX&id=600679765418&rn=40e69d907b6b5714d9dc71f287d06f56&abbucket=5'
# url = 'https://detail.tmall.com/item.htm?spm=a220m.1000858.1000725.35.46743d86dYQErX&id=528898859057&user_id=604339413&cat_id=2&is_b=1&rn=550647787b1bb2179dbafd440f34b6ca'
def getUrlVal(name,url):
    query = urlparse(str.encode(url)).query
    res = dict([(k,v[0]) for k,v in parse_qs(query).items()])
    return  bytes.decode(res[str.encode(name)])
goodsId = getUrlVal("id",url)
flag = checkGoods(goodsId)
if flag==0:
    goods_table_id = getGoodsDetail(goodsId,url);
    for i in range(1,101):
       flag = getComments(goodsId,goods_table_id,i)
       if flag==111111:
           break
else:
    print("========该商品数据已经存在了=======")
# getComments(goodsId,goods_table_id,1)  #测试

# # pubDate=html.find_all('script')[4].get_text() script